<!DOCTYPE html>
<html>
  <head>
    <title>017_00节流的原理和使用</title>
  </head>
  <style></style>
  <body>
    <div class="src">
      <div class="dragabled">
        <div class="txt" id="txt">
          <p draggable="true">此段文字设置了属性draggable="true"</p>
        </div>
      </div>
    </div>
    <script>
      var dragSrc = document.getElementById("txt");
      // 拖动开始
      dragSrc.ondragstart = function (event) {
        console.log("拖动开始", event);
      };
      /**
       * fn 待转换的方法
       * gapTime 方法执行的频率  间隔执行的时间
      */
      function throttle (fn, gapTime) {
        gapTime = gapTime || 500;
        var canRun = true; // 上次执行的时间
        return function () {
          var args = arguments;
          var context = this;
          if (!canRun) {
            return
          }
          canRun = false
          setTimeout(function () {
            fn.apply(context, args)
            canRun = true
          }, gapTime);
        }
      }
      // 拖动中
      var throttledFunction = throttle(function (a, b) {
        console.log("拖动中", a, b);
      })
      dragSrc.ondrag = function (event) { // 这里我们这样传递参数
        throttledFunction.call(this, event, 1)
      }
      dragSrc.ondragend = function () {
        // 拖动结束
        console.log("拖动结束");
      };
    </script>
  </body>
</html>
